#include<iostream>
#include<string>
#include<unistd.h>
#include<vector>
#include"Task.hpp"
#include"Channel.hpp"
#include<functional>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
using work_t =function<void()>;

enum{
    OK=0,
    UsageError,
    PipeError,
    ForkError
};

class ProcessPool
{
    public:
    ProcessPool(work_t w,int n)
    :processnum(n)
    ,work(w)
    {
        

    }
    int InitProcessPool()
    {
        for(int i=0;i<processnum;i++)
        {
            int fsd[2]={0};
            int n=pipe(fsd);
            if(n<0)
            {
                return PipeError;
            }
            pid_t id=fork();
            if(id<0)
            {
                return ForkError;
            }
            if(id==0)
            {
                //子进程
                //首先要关闭对应的读端
                std::cout << getpid() << ", child close history fd: ";
                //close(fsd[0]);如果想要进行关闭一个子进程 然后回收一个，就需要关闭每个子进程
                //因为拷贝父进程而多余指向管道的子进程
                for(auto& e:channels)
                {
                    e.Close();
                }
                //work();
            }
            else
            {
                
            }
        }
    }
 

private:
vector<Channel> channels;
int processnum;
work_t work;





};
